package com.example.demo.service.impl;


import com.example.demo.dao.PostDao;
import com.example.demo.model.Post;
import com.example.demo.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostDao PostDao;

    @Override
    public List<Post> findPost() {
        List<Post> Posts = PostDao.selectAll();
        return Posts;
    }

    @Override
    public void add(Post Post, String name) {
        Post.setOwnName(name);
        Post.setCreateTime(new Date());
        Post.setUpdateTime(new Date());
        System.out.println(Post);
        PostDao.insertSelective(Post);
    }

    @Override
    public void update(Post Post, Integer id) {
        Post.setPostId(id);
        PostDao.updateByPrimaryKeySelective(Post);
    }

    @Override
    public void delete(Integer id) {
        PostDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<Post> selectByUsername(String name) {
        List<Post> Posts = PostDao.selectByExample(name);
        return Posts;
    }

    @Override
    public Post selectById(Integer id) {
        Post Post = PostDao.selectByPrimaryKey(id);
        return Post;
    }

    @Override
    public List<Post> findPostByKeys(String keys) {
        List<Post> Posts = PostDao.selectAllByKeys(keys);
        return Posts;
    }

    @Override
    public Post selectByTitle(String title) {
        Post post = PostDao.findByTitle(title);
        return post;
    }
}
